//
//  DetailInformationController.m
//  ybzj
//
//  Created by liyuan on 15/7/14.
//  Copyright (c) 2015年 liyuan. All rights reserved.
//

#import "DetailInformationController.h"
#import "StarBar.h"
#import "DetailPageControl.h"

@interface DetailInformationController ()
@property (weak, nonatomic) IBOutlet UIScrollView *ImageScroll;
@property (weak, nonatomic) IBOutlet DetailPageControl *ImagePageControl;
@property (weak, nonatomic) IBOutlet UILabel *RoundLabel;
@property (weak, nonatomic) IBOutlet UIButton *StoreButton;
@property (weak, nonatomic) IBOutlet UILabel *TitleLabel;
@property (weak, nonatomic) IBOutlet StarBar *StarLabel;
@property (weak, nonatomic) IBOutlet UILabel *DistanceLabel;
@property (weak, nonatomic) IBOutlet UIView *BottomView;
@property (nonatomic) CGFloat contentOffsetX;



@end

@implementation DetailInformationController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    CGFloat width=[UIScreen mainScreen].bounds.size.width;
    if(self.clinicInfo==nil){
        self.clinicInfo=[[ClinicInfo alloc] init];
        self.clinicInfo.Title=@"拜尔口腔";
        self.clinicInfo.Star=2;
        self.clinicInfo.DetailButtonUrl=@"doctor4.jpg";
    }
   
    
    //set constrain for StoreButton
    self.StoreButton.translatesAutoresizingMaskIntoConstraints=NO;
    NSLayoutConstraint *topConstrainforStore =[NSLayoutConstraint constraintWithItem:self.StoreButton
                                                                           attribute:NSLayoutAttributeTop
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:self.view
                                                                           attribute:NSLayoutAttributeTop
                                                                          multiplier:1.0f
                                                                            constant:width*32/640];
    
    NSLayoutConstraint *rightConstrainforStore =[NSLayoutConstraint constraintWithItem:self.StoreButton
                                                                             attribute:NSLayoutAttributeTrailing
                                                                             relatedBy:NSLayoutRelationEqual
                                                                                toItem:self.view
                                                                             attribute:NSLayoutAttributeTrailing
                                                                            multiplier:1.0f
                                                                              constant:0-width*32/640];
    
    NSLayoutConstraint *equalConstrainforStore =[NSLayoutConstraint constraintWithItem:self.StoreButton
                                                                             attribute:NSLayoutAttributeWidth
                                                                             relatedBy:NSLayoutRelationEqual
                                                                                toItem:nil
                                                                             attribute:NSLayoutAttributeNotAnAttribute
                                                                            multiplier:1.0f
                                                                              constant:width*52/640];
    
    NSLayoutConstraint *heightConstrainforStore =[NSLayoutConstraint constraintWithItem:self.StoreButton
                                                                              attribute:NSLayoutAttributeHeight
                                                                              relatedBy:NSLayoutRelationEqual
                                                                                 toItem:nil
                                                                              attribute:NSLayoutAttributeNotAnAttribute
                                                                             multiplier:1.0f
                                                                               constant:width*45/640];
    
    topConstrainforStore.active=YES;
    rightConstrainforStore.active=YES;
    equalConstrainforStore.active=YES;
    heightConstrainforStore.active=YES;
    self.StoreButton.backgroundColor=[UIColor grayColor];
    
    //set constrain for scrollView
    self.ImageScroll.translatesAutoresizingMaskIntoConstraints=NO;
    NSLayoutConstraint *topConstrainforImageScroll=[NSLayoutConstraint constraintWithItem:self.ImageScroll
                                                                               attribute:NSLayoutAttributeTop
                                                                               relatedBy:NSLayoutRelationEqual
                                                                                  toItem:self.view
                                                                               attribute:NSLayoutAttributeTop
                                                                              multiplier:1.0f
                                                                                constant:0];
    NSLayoutConstraint *bottomConstrainforImageScroll=[NSLayoutConstraint constraintWithItem:self.ImageScroll
                                                                                attribute:NSLayoutAttributeBottom
                                                                                relatedBy:NSLayoutRelationEqual
                                                                                   toItem:self.view
                                                                                attribute:NSLayoutAttributeBottom
                                                                               multiplier:1.0f
                                                                                 constant:0];
    NSLayoutConstraint *leftConstrainforImageScroll=[NSLayoutConstraint constraintWithItem:self.ImageScroll
                                                                                attribute:NSLayoutAttributeLeading
                                                                                relatedBy:NSLayoutRelationEqual
                                                                                   toItem:self.view
                                                                                attribute:NSLayoutAttributeLeading
                                                                               multiplier:1.0f
                                                                                 constant:0];
    NSLayoutConstraint *widthConstrainforImageScroll=[NSLayoutConstraint constraintWithItem:self.ImageScroll
                                                                                attribute:NSLayoutAttributeWidth
                                                                                relatedBy:NSLayoutRelationEqual
                                                                                   toItem:self.view
                                                                                attribute:NSLayoutAttributeWidth
                                                                               multiplier:1.0f
                                                                                 constant:0];
    topConstrainforImageScroll.active=YES;
    bottomConstrainforImageScroll.active=YES;
    leftConstrainforImageScroll.active=YES;
    widthConstrainforImageScroll.active=YES;
    
    //set constrain for bottomView
    self.BottomView.translatesAutoresizingMaskIntoConstraints=NO;
    NSLayoutConstraint *topConstrainforBottomView=[NSLayoutConstraint constraintWithItem:self.BottomView
                                                                                attribute:NSLayoutAttributeTop
                                                                                relatedBy:NSLayoutRelationEqual
                                                                                   toItem:self.view
                                                                                attribute:NSLayoutAttributeTop
                                                                               multiplier:1.0f
                                                                                 constant:width*264/640];
    NSLayoutConstraint *bottomConstrainforBottomView=[NSLayoutConstraint constraintWithItem:self.BottomView
                                                                                   attribute:NSLayoutAttributeBottom
                                                                                   relatedBy:NSLayoutRelationEqual
                                                                                      toItem:self.view
                                                                                   attribute:NSLayoutAttributeBottom
                                                                                  multiplier:1.0f
                                                                                    constant:0];
    NSLayoutConstraint *leftConstrainforBottomView=[NSLayoutConstraint constraintWithItem:self.BottomView
                                                                                 attribute:NSLayoutAttributeLeading
                                                                                 relatedBy:NSLayoutRelationEqual
                                                                                    toItem:self.view
                                                                                 attribute:NSLayoutAttributeLeading
                                                                                multiplier:1.0f
                                                                                  constant:0];
    NSLayoutConstraint *rightConstrainforBottomView=[NSLayoutConstraint constraintWithItem:self.BottomView
                                                                                  attribute:NSLayoutAttributeTrailing
                                                                                  relatedBy:NSLayoutRelationEqual
                                                                                     toItem:self.view
                                                                                  attribute:NSLayoutAttributeTrailing
                                                                                 multiplier:1.0f
                                                                                   constant:0];
    topConstrainforBottomView.active=YES;
    bottomConstrainforBottomView.active=YES;
    leftConstrainforBottomView.active=YES;
    rightConstrainforBottomView.active=YES;
    self.BottomView.backgroundColor=[UIColor colorWithWhite:1 alpha:0.8];
    
    
    //set constrain for Title Label
    self.TitleLabel.translatesAutoresizingMaskIntoConstraints=NO;
    NSLayoutConstraint *topConstranintforTitleLabel = [NSLayoutConstraint constraintWithItem:self.TitleLabel
                                                                              attribute:NSLayoutAttributeTop
                                                                              relatedBy:NSLayoutRelationEqual
                                                                                 toItem:self.view
                                                                              attribute:NSLayoutAttributeTop
                                                                             multiplier:1.0f
                                                                               constant:width*298/640];
    
    NSLayoutConstraint *bottomConstrainforTitleLabel = [NSLayoutConstraint constraintWithItem:self.TitleLabel
                                                                               attribute:NSLayoutAttributeBottom
                                                                               relatedBy:NSLayoutRelationEqual
                                                                                  toItem:self.view
                                                                               attribute:NSLayoutAttributeBottom
                                                                              multiplier:1.0f
                                                                                constant:0-width*66/640];
    
    NSLayoutConstraint *leftConstrainforTitleLabel = [NSLayoutConstraint constraintWithItem:self.TitleLabel
                                                                             attribute:NSLayoutAttributeLeading
                                                                             relatedBy:NSLayoutRelationEqual
                                                                                toItem:self.view
                                                                             attribute:NSLayoutAttributeLeading
                                                                            multiplier:1.0f
                                                                              constant:width*32/640];
    
    NSLayoutConstraint *rightConstrainforTitleLabel = [NSLayoutConstraint constraintWithItem:self.TitleLabel
                                                                              attribute:NSLayoutAttributeWidth
                                                                              relatedBy:NSLayoutRelationEqual
                                                                                 toItem:nil
                                                                              attribute:NSLayoutAttributeNotAnAttribute
                                                                             multiplier:1.0f
                                                                               constant:width*400/640];
    
    topConstranintforTitleLabel.active=YES;
    bottomConstrainforTitleLabel.active=YES;
    leftConstrainforTitleLabel.active=YES;
    rightConstrainforTitleLabel.active=YES;
    //self.TitleLabel.text=self.clinicInfo.Title;
    NSMutableAttributedString *attributedTitle=[[NSMutableAttributedString alloc] initWithString:self.clinicInfo.Title];
    [attributedTitle addAttribute:NSForegroundColorAttributeName value:(id)[UIColor blackColor].CGColor range:NSMakeRange(0, [attributedTitle length])];
    [attributedTitle addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:width*32/640] range:NSMakeRange(0, [attributedTitle length])];
    [self.TitleLabel setAttributedText:attributedTitle];
     
    //set constrain for Star Label
    self.StarLabel.translatesAutoresizingMaskIntoConstraints=NO;
    NSLayoutConstraint *topConstranintforStar = [NSLayoutConstraint constraintWithItem:self.StarLabel
                                                                             attribute:NSLayoutAttributeTop
                                                                             relatedBy:NSLayoutRelationEqual
                                                                                toItem:self.view
                                                                             attribute:NSLayoutAttributeTop
                                                                            multiplier:1.0f
                                                                              constant:width*334/640];
    
    NSLayoutConstraint *bottomConstrainforStar = [NSLayoutConstraint constraintWithItem:self.StarLabel
                                                                              attribute:NSLayoutAttributeBottom
                                                                              relatedBy:NSLayoutRelationEqual
                                                                                 toItem:self.view
                                                                              attribute:NSLayoutAttributeBottom
                                                                             multiplier:1.0f
                                                                               constant:0-width*30/640];
    
    NSLayoutConstraint *leftConstrainforStar = [NSLayoutConstraint constraintWithItem:self.StarLabel
                                                                            attribute:NSLayoutAttributeLeading
                                                                            relatedBy:NSLayoutRelationEqual
                                                                               toItem:self.view
                                                                            attribute:NSLayoutAttributeLeading
                                                                           multiplier:1.0f
                                                                             constant:width*32/640];
    
    NSLayoutConstraint *lengthConstrainforStar = [NSLayoutConstraint constraintWithItem:self.StarLabel
                                                                              attribute:NSLayoutAttributeWidth
                                                                              relatedBy:NSLayoutRelationEqual
                                                                                 toItem:self.StarLabel
                                                                              attribute:NSLayoutAttributeHeight
                                                                             multiplier:7.0f
                                                                               constant:0];
    topConstranintforStar.active=YES;
    bottomConstrainforStar.active=YES;
    leftConstrainforStar.active=YES;
    lengthConstrainforStar.active=YES;
    [self.StarLabel setLevel:self.clinicInfo.Star];
    
    //set constrain for Round Label
    self.RoundLabel.translatesAutoresizingMaskIntoConstraints=NO;
    NSLayoutConstraint *leftConstraintforRoundLabel = [NSLayoutConstraint constraintWithItem:self.RoundLabel
                                                                      attribute:NSLayoutAttributeLeading
                                                                      relatedBy:NSLayoutRelationEqual
                                                                         toItem:self.view
                                                                      attribute:NSLayoutAttributeLeading
                                                                     multiplier:1.0f
                                                                       constant:width*508/640];
    
    
    
    NSLayoutConstraint* topConstraintforRoundLabel = [NSLayoutConstraint constraintWithItem:self.RoundLabel
                                                                     attribute:NSLayoutAttributeBottom
                                                                     relatedBy:NSLayoutRelationEqual
                                                                        toItem:self.BottomView
                                                                     attribute:NSLayoutAttributeTop
                                                                    multiplier:1.0f
                                                                      constant:width*48/640];
    
    
    NSLayoutConstraint* heightConstraintforRoundLabel = [NSLayoutConstraint constraintWithItem:self.RoundLabel
                                                                        attribute:NSLayoutAttributeHeight
                                                                        relatedBy:NSLayoutRelationEqual
                                                                           toItem:nil
                                                                        attribute:NSLayoutAttributeNotAnAttribute
                                                                       multiplier:1.0f
                                                                         constant:width*96/640];
    NSLayoutConstraint* equalConstraintforRoundLabel = [NSLayoutConstraint constraintWithItem:self.RoundLabel
                                                                                     attribute:NSLayoutAttributeHeight
                                                                                     relatedBy:NSLayoutRelationEqual
                                                                                        toItem:self.RoundLabel
                                                                                     attribute:NSLayoutAttributeWidth
                                                                                    multiplier:1.0f
                                                                                      constant:0];
    
    
    
    
    
    
    leftConstraintforRoundLabel.active = YES;
    topConstraintforRoundLabel.active = YES;
    heightConstraintforRoundLabel.active = YES;
    equalConstraintforRoundLabel.active=YES;
    self.RoundLabel.layer.cornerRadius=width*48/640;
    self.RoundLabel.layer.masksToBounds=YES;
    self.RoundLabel.layer.borderColor=[[UIColor whiteColor]CGColor];
    self.RoundLabel.layer.borderWidth= 2.0f;
    UIImageView *imageview=[[UIImageView alloc] initWithImage:[UIImage imageNamed: self.clinicInfo.DetailButtonUrl]];
    [self.RoundLabel addSubview:imageview];
    
    //set constrain for Distance Label
    self.DistanceLabel.translatesAutoresizingMaskIntoConstraints=NO;
    NSLayoutConstraint *topConstrainforDistance=[NSLayoutConstraint constraintWithItem:self.DistanceLabel
                                                                             attribute:NSLayoutAttributeTop
                                                                             relatedBy:NSLayoutRelationEqual
                                                                                toItem:self.view
                                                                             attribute:NSLayoutAttributeTop
                                                                            multiplier:1.0f
                                                                              constant:width*325/640];
    
    NSLayoutConstraint *bottomConstrainforDistance =[NSLayoutConstraint constraintWithItem:self.DistanceLabel
                                                                                 attribute:NSLayoutAttributeBottom
                                                                                 relatedBy:NSLayoutRelationEqual
                                                                                    toItem:self.view
                                                                                 attribute:NSLayoutAttributeBottom
                                                                                multiplier:1.0f
                                                                                  constant:0-width*30/640];
    
    NSLayoutConstraint *leftConstrainforDistance = [NSLayoutConstraint constraintWithItem:self.DistanceLabel
                                                                                attribute:NSLayoutAttributeLeading
                                                                                relatedBy:NSLayoutRelationEqual
                                                                                   toItem:self.view
                                                                                attribute:NSLayoutAttributeLeading
                                                                               multiplier:1.0f
                                                                                 constant:width*472/640];
    
    NSLayoutConstraint *rightConstrainforDistance = [NSLayoutConstraint constraintWithItem:self.DistanceLabel
                                                                                 attribute:NSLayoutAttributeTrailing
                                                                                 relatedBy:NSLayoutRelationEqual
                                                                                    toItem:self.view
                                                                                 attribute:NSLayoutAttributeTrailing
                                                                                multiplier:1.0f
                                                                                  constant:0];
    bottomConstrainforDistance.active=YES;
    topConstrainforDistance.active=YES;
    leftConstrainforDistance.active=YES;
    rightConstrainforDistance.active=YES;
    self.DistanceLabel.text=@"10.1km";
    [self.DistanceLabel setTextAlignment:NSTextAlignmentCenter];
    self.DistanceLabel.font=[UIFont boldSystemFontOfSize:width*25/640];
    self.DistanceLabel.textColor=[UIColor colorWithRed:145/255.0 green:146/255.0 blue:147/255.0 alpha:1];
    
    //set page control view
    self.ImagePageControl.translatesAutoresizingMaskIntoConstraints=NO;
    NSLayoutConstraint *leftConstraintforImagePageControl = [NSLayoutConstraint constraintWithItem:self.ImagePageControl
                                                                                   attribute:NSLayoutAttributeLeading
                                                                                   relatedBy:NSLayoutRelationEqual
                                                                                      toItem:self.view
                                                                                   attribute:NSLayoutAttributeLeading
                                                                                  multiplier:1.0f
                                                                                    constant:width*32/640];
    
    
    
    NSLayoutConstraint* bottomConstraintforImagePageControl = [NSLayoutConstraint constraintWithItem:self.ImagePageControl
                                                                                  attribute:NSLayoutAttributeBottom
                                                                                  relatedBy:NSLayoutRelationEqual
                                                                                     toItem:self.view
                                                                                  attribute:NSLayoutAttributeBottom
                                                                                 multiplier:1.0f
                                                                                   constant:0-width*136/640];
    
    
    NSLayoutConstraint* heightConstraintforImagePageControl = [NSLayoutConstraint constraintWithItem:self.ImagePageControl
                                                                                     attribute:NSLayoutAttributeHeight
                                                                                     relatedBy:NSLayoutRelationEqual
                                                                                        toItem:nil
                                                                                     attribute:NSLayoutAttributeNotAnAttribute
                                                                                    multiplier:1.0f
                                                                                      constant:width*10/640];
    NSLayoutConstraint* rightConstraintforImagePageControl = [NSLayoutConstraint constraintWithItem:self.ImagePageControl
                                                                                    attribute:NSLayoutAttributeTrailing
                                                                                    relatedBy:NSLayoutRelationEqual
                                                                                       toItem:self.view
                                                                                    attribute:NSLayoutAttributeTrailing
                                                                                   multiplier:1.0f
                                                                                     constant:0-width*550/640];
    
    leftConstraintforImagePageControl.active=YES;
    bottomConstraintforImagePageControl.active=YES;
    heightConstraintforImagePageControl.active=YES;
    rightConstraintforImagePageControl.active=YES;
    
    self.ImageScroll.pagingEnabled = YES;
    self.ImageScroll.contentSize = CGSizeMake(width * self.ImageUrlList.count,0);
    self.ImageUrlList=[NSArray arrayWithObjects:@"ClinicList_06.jpg",@"ClinicList_04.jpg",@"ClinicList_02.jpg", nil];
    //set background images

    
    

}

-(void)viewWillAppear:(BOOL)animated{
    CGFloat width=[UIScreen mainScreen].bounds.size.width;;
    self.ImageScroll.contentSize = CGSizeMake(width * self.ImageUrlList.count,0);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setImageUrlList:(NSArray *)ImageUrlList{
    CGFloat width=[UIScreen mainScreen].bounds.size.width;
    if(!_ImageUrlList){
        _ImageUrlList=[[NSArray alloc] initWithArray:ImageUrlList];
    }
    _ImageUrlList=ImageUrlList;
    
    [self.ImagePageControl setNumberOfPages:_ImageUrlList.count];
    [self.ImagePageControl setCurrentPage:0];
    
    for(UIView *view in self.ImageScroll.subviews){
        if([view isKindOfClass:[UIView class]]){
            [view removeFromSuperview];
        }
    }
    
    for(int i=0; i< ImageUrlList.count;i++){
       
        //UIImageView *imageview=[[UIImageView alloc] initWithImage:[UIImage imageNamed:[self.ImageUrlList objectAtIndex:i]]];
        UIView *subview=[[UIView alloc] initWithFrame:CGRectMake(width*i, 0, width, self.ImageScroll.bounds.size.height)];
        [subview setBackgroundColor: [UIColor colorWithPatternImage: [UIImage imageNamed:[self.ImageUrlList objectAtIndex:i]]]];
        [self.ImageScroll addSubview:subview];
        
    }
    
    
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

{
    
    self.contentOffsetX= self.ImageScroll.contentOffset.x;
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{
    //[self.ImageScroll setContentOffset: CGPointMake(self.ImageScroll.contentOffset.x, 0)];
    if(self.ImageScroll.contentOffset.x>self.contentOffsetX){
        self.StoreButton.backgroundColor=[UIColor redColor];
        
    }
    else{
         self.StoreButton.backgroundColor=[UIColor grayColor];    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self.ImagePageControl  changeCurrentPage:self.ImageScroll.contentOffset.x/self.ImageScroll.bounds.size.width];
     }

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
